using System;
using System.ComponentModel.Composition;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICP_MS.Plugin.LA300.Properties;

namespace ICP_MS.Plugin.LA300;

public class LA300Plugin : Extension
{
	private const string kStart = "kStart";

	private const string kStop = "kStop";

	private SimpleActionItem start;

	private SimpleActionItem stop;

	[Import("Shell", typeof(ContainerControl))]
	private ContainerControl Shell { get; set; }

	public override void Activate()
	{
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Expected O, but got Unknown
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Expected O, but got Unknown
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Expected O, but got Unknown
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Expected O, but got Unknown
		//IL_01f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Expected O, but got Unknown
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_022f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0240: Expected O, but got Unknown
		//IL_025d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0262: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: Expected O, but got Unknown
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d9: Expected O, but got Unknown
		IHeaderControl header = ((Extension)this).App.HeaderControl;
		ref SimpleActionItem reference = ref start;
		HeaderItem headerItem = header.GetHeaderItem("kStart");
		reference = (SimpleActionItem)(object)((headerItem is SimpleActionItem) ? headerItem : null);
		if (start == null)
		{
			start = new SimpleActionItem(Lang.Txt("iMass.Experiment.Start.Text"), (EventHandler)Experiment_Start)
			{
				Key = "kStart",
				Direction = (Direction)0,
				GroupCaption = "Control",
				LargeImage = Resources.Run,
				SmallImage = Resources.Run,
				Size = 55,
				Enabled = true
			};
			header.Add((HeaderItem)(object)start);
		}
		ref SimpleActionItem reference2 = ref stop;
		HeaderItem headerItem2 = header.GetHeaderItem("kStop");
		reference2 = (SimpleActionItem)(object)((headerItem2 is SimpleActionItem) ? headerItem2 : null);
		if (stop == null)
		{
			stop = new SimpleActionItem(Lang.Txt("iMass.Experiment.Stop.Text"), (EventHandler)Experiment_Stop)
			{
				Key = "kStop",
				Direction = (Direction)0,
				GroupCaption = "Control",
				LargeImage = Resources.Stop,
				SmallImage = Resources.Stop,
				Size = 55
			};
			header.Add((HeaderItem)(object)stop);
		}
		header.Add((HeaderItem)new RootItem("kHome", Lang.Txt("iMass.Menu.File.Text"))
		{
			SortOrder = -100
		});
		if (!header.CheckKey("kNew"))
		{
			header.Add((HeaderItem)new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.New.Text"), (EventHandler)null)
			{
				Key = "kNew"
			});
		}
		if (!header.CheckKey("kOpen"))
		{
			header.Add((HeaderItem)new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.Open.Text"), (EventHandler)null)
			{
				Key = "kOpen"
			});
		}
		header.Add((HeaderItem)new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.New.LA.Text"), (EventHandler)Experiment_New)
		{
			MenuContainerKey = "kNew"
		});
		header.Add((HeaderItem)new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.New.LAFromExisted.Text"), (EventHandler)Experiment_NewFromOld)
		{
			MenuContainerKey = "kNew"
		});
		header.Add((HeaderItem)new SimpleActionItem("kHome", Lang.Txt("iMass.Menu.File.Open.LA300.Text"), (EventHandler)Experiment_Open)
		{
			MenuContainerKey = "kOpen"
		});
		((Extension)this).Activate();
		Shell = ((Extension)this).App.Shell;
	}

	public override void Deactivate()
	{
		((Extension)this).Deactivate();
	}

	private void Experiment_New(object sender, EventArgs e)
	{
		OpenFileDialog dlg = new OpenFileDialog();
		dlg.Filter = Lang.Txt("iMass.Experiment.New.DlgFilter.Text");
		dlg.Title = Lang.Txt("iMass.Experiment.New.DlgTitle.Text");
		if (dlg.ShowDialog() == DialogResult.OK)
		{
			string tempDB = GetTempDB(dlg.FileName);
			OpenExperimentDB(tempDB);
		}
	}

	private void Experiment_NewFromOld(object sender, EventArgs e)
	{
		OpenFileDialog dlg = new OpenFileDialog();
		dlg.Filter = Lang.Txt("iMass.Experiment.NewFromOldLA.DlgFilter.Text");
		dlg.Title = Lang.Txt("iMass.Experiment.NewFromOld.DlgTitle.Text");
		if (dlg.ShowDialog() == DialogResult.OK)
		{
			string tempDB = GetTempDB(dlg.FileName);
			OpenExperimentDB(tempDB);
		}
	}

	private void Experiment_Open(object sender, EventArgs e)
	{
		using OpenFileDialog dlg = new OpenFileDialog();
		dlg.Filter = Lang.Txt("iMass.Experiment.OpenLA.DlgFilter.Text");
		dlg.Title = Lang.Txt("iMass.Experiment.Open.DlgTitle.Text");
		if (dlg.ShowDialog() == DialogResult.OK)
		{
			OpenExperimentDB(dlg.FileName, isOpen: true);
		}
	}

	private void Experiment_Start(object sender, EventArgs e)
	{
		if ((Shell as Form).ActiveMdiChild is IExperiment exp && exp.PreRun())
		{
			((ActionItem)start).Enabled = false;
			exp.StartAnalysis();
			object tag = (Shell as Form).Tag;
			UpdateExpHeaderIcon(exp, (SimpleActionItem)((tag is SimpleActionItem) ? tag : null));
		}
	}

	private void Experiment_Stop(object sender, EventArgs e)
	{
		Form[] mdiChildren = (Shell as Form).MdiChildren;
		foreach (Form form in mdiChildren)
		{
			if (form is IExperiment expForm)
			{
				expForm.StopAnalysis();
			}
		}
		((ActionItem)start).Enabled = true;
	}

	private void OpenExperimentDB(string path, bool isOpen = false)
	{
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Expected O, but got Unknown
		string tempName = Path.GetFileNameWithoutExtension(path);
		FormExperiment form = new FormExperiment(path, isOpen);
		SimpleActionItem headItem = new SimpleActionItem(tempName, (EventHandler)delegate
		{
			if (form != null)
			{
				form.Activate();
				form.BringToFront();
			}
		})
		{
			LargeImage = Resources.NewExperiment,
			SmallImage = Resources.NewExperiment,
			Direction = (Direction)1,
			ToggleGroupKey = "kLeftBar",
			GroupCaption = "Advance",
			Size = 72
		};
		form.Tag = headItem;
		((Extension)this).App.HeaderControl.Add((HeaderItem)(object)headItem);
		form.FormClosed += delegate
		{
			((Extension)this).App.HeaderControl.Remove(((HeaderItem)headItem).Key);
			form.Dispose();
			form = null;
			GC.Collect();
		};
		form.Activated += delegate
		{
			headItem.Toggle();
		};
		form.updateToolstrip += delegate
		{
			string text = form.Text;
			if (form.Text.Length > 5)
			{
				text = text.Substring(0, 4) + "...";
			}
			((ActionItem)headItem).Caption = text;
			((ActionItem)headItem).ToolTipText = form.Text;
		};
		form.analyseFinish += delegate
		{
			((ActionItem)start).Enabled = true;
			UpdateExpHeaderIcon(form, headItem);
		};
		form.ExpEvent += delegate(object sender, ExpProgressArgs e)
		{
			((IProgressHandler)((Extension)this).App.StatusControl).Progress((string)null, e.Seconds, e.Text);
		};
		form.Text = tempName;
		form.MdiParent = Shell as Form;
		form.Show();
		if (!FormExperiment.IsRun)
		{
			((ActionItem)start).Enabled = true;
		}
	}

	private void UpdateExpHeaderIcon(IExperiment exp, SimpleActionItem item)
	{
		if (exp != null && item != null)
		{
			Image image = Resources.Experiment;
			switch (exp.CurrentStatus)
			{
			case 0:
				image = Resources.NewExperiment;
				break;
			case 1:
				image = Resources.CurrentExperiment;
				break;
			case 2:
				image = Resources.NewExperiment;
				break;
			case 3:
				image = Resources.Experiment;
				break;
			}
			item.LargeImage = image;
		}
	}

	private string GetTempDB(string anlytePath)
	{
		string tempPreName = Path.GetTempPath() + Lang.Txt("FormScan.Experiment.Text");
		string temp = null;
		int count = 1;
		while (true)
		{
			temp = tempPreName + count + ".ncs";
			if (!File.Exists(temp))
			{
				break;
			}
			count++;
		}
		File.Copy(anlytePath, temp);
		return temp;
	}
}
